#------------------------
# Combine with ques
# Date: 2024-04-05
#------------------------

library(data.table)
library(dplyr)
library(survival)
library(WeightIt)
library(cobalt)
library(epiR)
# load df
load("./Data/analysis.Rdata")

# Load the ques
ques <- haven::read_sas("./Data/ques2.sas7bdat")
ques <- as.data.table(ques)

# Combine
df_all <- inner_join(df, ques, by = c("name", "birth"), multiple = "any")
df_all <- df_all[semster <= 18, ]
df_all <- df_all[section == "Six central districts", ]
nrow(df_all)
# varibale redefine
df_all <- df_all[, .(
    time, event, sex, outdoor, familyHistory, outdoor,
    baseSE, semster, section, nearDisPlay, nearDisRead,
    nearDisStudy
)]
df_all <- df_all[, `:=`(
    outdoor1 = if_else(outdoor <= 2, 1, 0),
    nearDisRead1 = if_else(nearDisRead > 2, 1, 0),
    nearDisPlay1 = if_else(nearDisPlay > 2, 1, 0),
    nearDisStudy1 = if_else(nearDisStudy > 2, 1, 0)
)]
df_all <- df_all[, `:=`(
    outdoor = pmin(outdoor, 3),
    nearDisRead = pmin(nearDisRead, 3),
    nearDisPlay = pmin(nearDisPlay, 3),
    nearDisStudy = pmin(nearDisStudy, 3)
)]

# LifeStyle
df_all[, `:=`(
    outdoor1f = factor(outdoor),
    nearDisRead1f = factor(nearDisRead),
    nearDisPlay1f = factor(nearDisPlay),
    nearDisStudy1f = factor(nearDisStudy)
)]
fit_cox <- coxph(Surv(time, event) ~ outdoor1f + semster
                + familyHistory + baseSE +  nearDisRead1f
                + nearDisStudy1f + sex,
    # weights = weight_it_ebal$weights,
    robust = T,
    ties = "efron", data = df_all
)
summary(fit_cox)
# Interaction Assessment
# outdoor & nearDisRead # nolint
df_all[, trt1 := fcase(
    outdoor1 == 0 & nearDisRead1 == 0, 0,
    outdoor1 == 0 & nearDisRead1 == 1, 1,
    outdoor1 == 1 & nearDisRead1 == 0, 2,
    outdoor1 == 1 & nearDisRead1 == 1, 3
)]
df_all[, trt1 := factor(trt1)]
# cal the weight
# ebal
fit_cox <- coxph(
    Surv(time, event) ~ trt1 + semster + familyHistory + baseSE,
    ties = "efron", data = df_all,robust = T
)
summary(fit_cox)
epi.interaction(fit_cox, param = "dummy", coef = c(1, 2, 3))

# outdoor & nearDisPlay # nolint
df_all[, trt2 := fcase(
    outdoor1 == 0 & nearDisStudy1 == 0, 0,
    outdoor1 == 0 & nearDisStudy1 == 1, 1,
    outdoor1 == 1 & nearDisStudy1 == 0, 2,
    outdoor1 == 1 & nearDisStudy1 == 1, 3
)]
df_all[, trt2 := factor(trt2)]
# cal the weight
# ebal
fit_cox <- coxph(Surv(time, event) ~ trt2 + semster +
                + familyHistory + baseSE,
    robust = TRUE, ties = "efron", data = df_all
)
summary(fit_cox)
epi.interaction(fit_cox, param = "dummy", coef = c(1, 2, 3))

# outdoor & nearDisPlay # nolint
df_all[, trt3 := fcase(
    nearDisRead1 == 0 & nearDisStudy1 == 0, 0,
    nearDisRead1 == 0 & nearDisStudy1 == 1, 1,
    nearDisRead1 == 1 & nearDisStudy1 == 0, 2,
    nearDisRead1 == 1 & nearDisStudy1 == 1, 3
)]
df_all[, trt3 := factor(trt3)]
# cal the weight
fit_cox <- coxph(Surv(time, event) ~ trt3 + semster +
                familyHistory + baseSE,
    ties = "efron", data = df_all
)
summary(fit_cox)
epi.interaction(fit_cox, param = "dummy", coef = c(1, 2, 3))
